#!/bin/sh
EDGE_CORE_INSTALL_PATH=/var/alog/AtlasEdge_log/
EDGE_CORE_INSTALL_FILE=atlasedge_script.log
MODE_NAME="atlasedge"

date_format="+%Y-%m-%dT%T"

# Some useful colors.
# check if stdout is a terminal and support colors...
if [ -t 1 ] && [ "1$(tput colors 2>/dev/null)" -ge 18 ]; then
  readonly color_red="$(tput setaf 1)"
  readonly color_yellow="$(tput setaf 3)"
  readonly color_green="$(tput setaf 2)"
  readonly color_norm="$(tput sgr0)"
else 
  readonly color_red=""
  readonly color_yellow=""
  readonly color_green=""
  readonly color_norm=""
fi

if command -v caller >/dev/null 2>&1; then
  # return func(lineno:filename)
  # NOTE: skip 2-level inner frame
  _caller() { caller 2| awk '{sub(/.*\//,e,$3);print $2"("$3":"$1") "}'; }
else
  _caller() { :; }
fi

if [ ! -d ${EDGE_CORE_INSTALL_PATH} ]; then
  mkdir -p ${EDGE_CORE_INSTALL_PATH} && chmod -R 700 ${EDGE_CORE_INSTALL_PATH}
fi
if [ ! -f ${EDGE_CORE_INSTALL_PATH}/${EDGE_CORE_INSTALL_FILE} ]; then
  touch ${EDGE_CORE_INSTALL_PATH}/${EDGE_CORE_INSTALL_FILE} && chmod 600 ${EDGE_CORE_INSTALL_PATH}/${EDGE_CORE_INSTALL_FILE}
fi

_log() 
{
  level=$1
  shift 1
  local cur_user=$(whoami)
  if [ ${cur_user} == "HwHiAiUser" ]; then
    echo "$(date ${date_format}) -${MODE_NAME}- ${level} $(_caller)- $*" >> ${EDGE_CORE_INSTALL_PATH}/${EDGE_CORE_INSTALL_FILE}
    echo "$(date ${date_format}) -${MODE_NAME}- ${level} $(_caller)- $*"
  else
    sudo -u HwHiAiUser bash -c "echo '$(date ${date_format}) -${MODE_NAME}- ${level} $(_caller)- $*' >> ${EDGE_CORE_INSTALL_PATH}/${EDGE_CORE_INSTALL_FILE}" 2>/dev/null
    sudo -u HwHiAiUser bash -c "echo '$(date ${date_format}) -${MODE_NAME}- ${level} $(_caller)- $*'" 2>/dev/null
  fi
}

logger_Debug() 
{
  echo "$@"
}

logger_Info() 
{
  _log INFO "$@"
}

logger_Warn() 
{
  _log WARN "${color_yellow}$*${color_norm}"
}

logger_Error() 
{
  _log ERROR "${color_red}$*${color_norm}"
}

die() 
{
  _log ERROR "${color_red}$*${color_norm}"
  exit 1
}